<?php
/*
 * 手机客户端店铺-购物清单接口
 * 2014-03-10
 * 2014-05-08 (已查) 
 */
include "inc/public_header.php";

include "func/user.php";				//用户信息处理函数


$reJson = array(); //构建返回值

$json = $_REQUEST["json"];
//$json 		= '{"address": [{"name": "\u91d1\u4e1c\u56fd","province": "\u4e0a\u6d77\u5e02","city": "\u4e0a\u6d77","county": "\u666e\u9640\u533a","detailAddress": "\u5149\u590d\u897f\u8def","postCode": "","mobile": "13564838623","phone": "-"}],"list":[{"goods":[{"goodsNum":"4","goodsId":"60"},{"goodsNum":"3","goodsId":"69"}],"shopId":"66"},{"goods":[{"goodsNum":"2","goodsId":"70"},{"goodsNum":"1","goodsId":"72"}],"shopId":"67"}]}';		

//获取用户信息
$json = $_REQUEST["uid"];



//$uid = "10279";

$reJson["success"] = "1";
$arr=json_decode($json,true);
if ($arr['address']){
	if ($arr['address']['uaid']){
	$sql = "update user_addresses set receiveName='".$arr['address']['name']."',province='".$arr['address']['province']."',city='".$arr['address']['city']."',district='".$arr['address']['county']."',street='".$arr['address']['detailAddress']."',zipcode='".$arr['address']['postCode']."',mobile='".$arr['address']['mobile']."',phone='".$arr['address']['phone']."' where uaid='".$arr['address']['uaid']."'";
	$db->query($sql);
	}else{
	$sql = "insert into user_addresses (uid,receiveName,province,city,district,street,zipcode,mobile,phone,createtime) values('".$uid."','".$arr['address']['name']."','".$arr['address']['province']."','".$arr['address']['city']."','".$arr['address']['county']."','".$arr['address']['detailAddress']."','".$arr['address']['postCode']."','".$arr['address']['mobile']."','".$arr['address']['phone']."',now())";
	$db->query($sql);
	}	
}

//获取地址
$sql = "select uaid,receiveName,province,city,district,street,zipcode,mobile,phone from `user_addresses` where  uid='".$uid."' ORDER BY uaid desc limit 1"; 
$db->query($sql);
if($db->next_record()){
	$uaid			= $db->f("uaid");
	$receiveName	= $db->f("receiveName");
	$province 		= $db->f("province");
	$city	  		= $db->f("city");
	$district	   	= $db->f("district");
	$street	   		= $db->f("street");
	$zipcode	   	= $db->f("zipcode");
	$mobile	   		= $db->f("mobile");
	$phone	   		= $db->f("phone");
}
if ($receiveName){
$address = array();
$address = array(
		"uaid"=>(string)$uaid
		,"name"=>(string)$receiveName
		,"province"=>(string)$province
		,"city"=>(string)$city
		,"county"=>(string)$district
		,"detailAddress"=>(string)$street
		,"postCode"=>(string)$zipcode
		,"mobile"=>(string)$mobile
		,"phone"=>(string)$phone
	);
$reJson["address"] = $address;
}

$shopList=array();
 
foreach($arr['list'] as $val){
	$arr2['shopId']=$val['shopId'];
	
	$arr3=$val['goods'];
	foreach($arr3 as $v){
		
		$y="SELECT b.ems_a*a.weight*'".$v['goodsNum']."' as tems,b.kd_a*a.weight*'".$v['goodsNum']."' as tkd,b.py_a*a.weight*'".$v['goodsNum']."' as tpy,b.ems_f,b.kd_f,b.py_f,b.ems_a,b.kd_a,b.py_a from shop_product a
							INNER JOIN
							shop_region_area b on a.shop_id=b.shop_id
							inner join 
							region c on b.area_id=c.area_id
							where a.product_id='".$v['goodsId']."' and a.shipping='0' and c.codes like '%0000' and c.region='".$province."'";
		$db2->query($y);
		 if($db2->next_record()){
			$tems = $db2->f('tems');
			$tkd = $db2->f('tkd');
			$tpy = $db2->f('tpy');
			$ems_f = $db2->f('ems_f');
			$kd_f = $db2->f('kd_f');
			$py_f = $db2->f('py_f');
			$ems_a = $db2->f('ems_a');
			$kd_a = $db2->f('kd_a');
			$py_a = $db2->f('py_a');
		 }
		 $arr4[]=$tems;
		 $arr5[]=$tkd;
		 $arr6[]=$tpy;
		 $tems="";
		 $tkd="";
		 $tpy="";
	}
	$ttems=array_sum($arr4)+$ems_f-$ems_a;
	$ttkd=array_sum($arr5)+$kd_f-$kd_a;
	$ttpy=array_sum($arr6)+$py_f-$py_a;
	$ems_f="";
	$kd_f="";
	$py_f="";
	$ems_a="";
	$kd_a="";
	$py_a="";
	$arr4="";
	$arr5="";
	$arr6="";
	$carriage=array();
	if ($ttems){
		$carriage[] = array(
			"type"=>"EMS"
			,"price"=>"$ttems"
		);
	}
	if ($ttkd){
		$carriage[] = array(
			"type"=>"快递"
			,"price"=>"$ttkd"
		);
	}
	if ($ttpy){
		$carriage[] = array(
			"type"=>"平邮"
			,"price"=>"$ttpy"
		);
	}
	if (!$ttems && !$ttkd && !$ttpy){
		$carriage[] = array(
			"type"=>"免邮"
			,"price"=>"0"
		);
	}
	$arr2['carriage']=$carriage;
	
	$shopList[]=$arr2;
	
	
		 
	
}
//print_r ($shopList); 
$reJson["shopList"] = $shopList;
echo json_encode($reJson);
/*$list=array();
$list1=array();
 
foreach($arr as $val){
    $sql = "select b.product_name,b.quantity,b.salesvolume,a.product_id from (SELECT product_id from shop_product_gwc where uid='".$uid."') a right join  shop_product  b on a.product_id=b.product_id where b.product_id = '".$val['goodsId']."'";
	$db1->query($sql);
	$db1->next_record();
	if (!$db1->f("product_id")){
		$list[]=$db1->f("product_name");
	}
	if (($db1->f("quantity"))-($db1->f("salesvolume"))<$val['goodsNum']){
		$list1[]=$db1->f("product_name");
	}
}
if ($list){
	echo json_encode(array("success"=>"0","errCode"=>"1","msg"=>"该商品已经不在购物车","list"=>$list));
	die;
}else if ($list1){
	echo json_encode(array("success"=>"0","errCode"=>"2","msg"=>"商品库存不足","list"=>$list1));
	die;
}else{
	echo json_encode(array("success"=>"1"));
	die;
}*/
?>
